//author: create by SunJH
//时间: 2021/11/17 0017
//邮箱: 965989158@qq.com
//描述: 测试webView网页加载

import 'package:flutter_app/counter/counter_net_loading.dart';
import 'package:flutter_app/utils/log_util.dart';
import 'package:flutter_inappwebview/flutter_inappwebview.dart';
import 'package:get/get.dart';

import 'package:flutter/material.dart';
class WebViewExample extends StatefulWidget {
  const WebViewExample({Key? key}) : super(key: key);

  @override
  _WebViewExampleState createState() => _WebViewExampleState();
}

class _WebViewExampleState extends State<WebViewExample> {
  @override
  Widget build(BuildContext context) {
    var controller = Get.put(CounterNetLoading());

    return  Scaffold(
        ///防止键盘把页面顶走
        resizeToAvoidBottomInset: false,
        body:SafeArea(
          child: WillPopScope(
            onWillPop: ()async{
              var canGoBack = await controller.webViewController?.canGoBack()??false;
              if(canGoBack){
                controller.webViewController?.goBack();
                return false;
              }else{
                return true;
              }
            },
            child:InAppWebView(
              initialOptions: controller.options,
              // initialUrlRequest: URLRequest(url: Uri.parse("https://test-ks.qimingdaren.com/#/login")),
              initialUrlRequest: URLRequest(url: Uri.parse("https://cn.bing.com")),
              onWebViewCreated: (webViewController){
                controller.webViewController = webViewController;
                controller.initMethod();
              },
              androidOnPermissionRequest: (controller, origin, resources) async {
                return PermissionRequestResponse(
                    resources: resources,
                    action: PermissionRequestResponseAction.GRANT);
              },
              onLoadStart: (controller,url){
                //开始加载页面
                LogUtil.logI("页面加载开始'url is : $url");
              },
              onLoadStop: (controller, url) async {
                //完成页面加载
                LogUtil.logI("页面加载结束'url is ：$url");
              },
              onLoadError: (controller, url, code, message) {
                LogUtil.logI("页面加载错误'url is ：$url");
              },
              onProgressChanged: (controller, progress) {
                LogUtil.logI("页面加载进度改变'progress is ：$progress");
              },
            ),
          ),
        ),
      );

  }

}
